package com.decent.ejfadmin.report.service.impl;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.report.dao.CollectCardStockReportDao;
import com.decent.ejfadmin.report.response.EstimateStockSupplyTimeDto;
import com.decent.ejfadmin.report.service.CollectCardStockReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weicx
 * @date 2018/08/01
 */
@Service
public class CollectCardStockReportServiceImpl implements CollectCardStockReportService {
    @Autowired
    private CollectCardStockReportDao collectCardStockReportDao;

    /**
     * 收卡卡密库存统计
     *
     * @param pageUnit    分页组件
     * @param productType 运营商
     * @param face        面值
     * @return Page
     */
    @Override
    public Page collectCardStockReportPage(Page pageUnit, Integer productType, String face) {
        int total = collectCardStockReportDao.collectCardStockReportCount(productType, face);
        pageUnit.setTotal(total);
        if (total == 0) {
            pageUnit.setResultList(new ArrayList());
        } else {
            pageUnit.setResultList(collectCardStockReportDao.collectCardStockReportPage(pageUnit, productType, face));
        }
        return pageUnit;
    }

    /**
     * 分页查询收卡库存折扣
     *
     * @param pageUnit    分页组件
     * @param productType 运营商
     * @param face        面值
     * @return 总数
     */
    @Override
    public Page collectCardStockPricePage(Page pageUnit, Integer productType, String face) {
        int total = collectCardStockReportDao.collectCardStockPriceCount(productType, face);
        pageUnit.setTotal(total);
        if (total == 0) {
            pageUnit.setResultList(new ArrayList());
        } else {
            pageUnit.setResultList(collectCardStockReportDao.collectCardStockPricePage(pageUnit, productType, face));
        }
        return pageUnit;
    }

    /**
     * 预估收卡库存销卡时间
     *
     * @param productType 运营商
     * @param face        面值
     * @param account     会员编号
     * @return list
     */
    @Override
    public List<EstimateStockSupplyTimeDto> estimateStockSupplyTime(Integer productType, String face, String account) {
        List<EstimateStockSupplyTimeDto> list = collectCardStockReportDao.estimateStockSupplyTime(productType, face);
        if (StringUtils.isNotBlank(account)) {
            for (EstimateStockSupplyTimeDto timeDto : list) {
                BigDecimal minUserPrice = collectCardStockReportDao.getMinUserPrice(account, productType, face, timeDto.getArea());
                if (minUserPrice != null) {
                    int frontCount = collectCardStockReportDao.getFrontCount(minUserPrice, productType, face, timeDto.getArea());
                    timeDto.setFrontCount(frontCount);
                }
            }
        }
        return list;
    }
}
